﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;

namespace ontheroad
{
    public partial class trfNews : PhoneApplicationPage
    {
        private List<trafficNews> linkList;

        public trfNews()
        {
            InitializeComponent();
            getIncident();
        }

        private void searchwherebut_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            setSearchIncident();
        }

        private void setSearchIncident()
        {
            string roadName = searchwheretxtbox.Text;

            List<trafficNews> cfc = new List<trafficNews>();

            for (int i = 0; i < linkList.Count; i++)
                if (linkList[i].location.Contains(roadName))
                    cfc.Add(linkList[i]);
            if (cfc.Count == 0)
                noticeMsgNotFound.Visibility = Visibility.Visible;
            else
                noticeMsgNotFound.Visibility = Visibility.Collapsed;
            trafficnewslistbox.ItemsSource = cfc;
        }

        public void getIncident()
        {
            linkList = new List<trafficNews>();
            WebClient wcf = new WebClient();
            linkList = new List<trafficNews>();
            Uri uri = new Uri("http://api.traffy.in.th/apis/apitraffy.php?api=getIncident&key="
                + App.traf.sessionkey + "&appid=" + App.traf.appid + "&format=XML&limit=30&type=all");
            wcf.DownloadStringAsync(uri);
            wcf.DownloadStringCompleted += (s, e) =>
            {
                XElement elm;
                try
                {
                    elm = XElement.Parse(e.Result);
                }

                catch (WebException)
                {
                    MessageBox.Show("Please check your internet connection.");
                    NavigationService.GoBack();
                    return;
                }
                catch (Exception)
                {
                    MessageBox.Show("Server connection failed, Please wait a minute and try again.");
                    NavigationService.GoBack();
                    return;
                }
                elm.FirstNode.Remove();
                while (elm.FirstNode != null)
                {
                    string title = (((elm.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).Value;
                    string source = (elm.FirstNode as XElement).FirstAttribute.NextAttribute.NextAttribute.Value;
                    string des = ((((elm.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).NextNode as XElement).Value;
                    string loctype = (((((elm.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).NextNode as XElement).NextNode as XElement).FirstAttribute.Value;
                    string loc = ((((((elm.FirstNode as XElement).FirstNode as XElement).NextNode as XElement).NextNode as XElement).NextNode as XElement).FirstNode as XElement).FirstAttribute.Value;
                    string firsttime = (elm.FirstNode as XElement).FirstAttribute.NextAttribute.NextAttribute.NextAttribute.Value;
                    string endtime = (elm.FirstNode as XElement).FirstAttribute.NextAttribute.NextAttribute.NextAttribute.NextAttribute.Value;
                    trafficNews news = new trafficNews();
                    news.title = title;
                    news.source = source;
                    news.description = des;
                    news.location = loc;
                    news.startTime = firsttime;
                    news.endTime = endtime;
                    linkList.Add(news);
                    elm.FirstNode.Remove();
                }
                if (linkList.Count == 0)
                    MessageBox.Show("NO information");
                trafficnewslistbox.ItemsSource = linkList;
                waitScr.Visibility = Visibility.Collapsed;
            };
        }

        private void searchwheretxtbox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            if (e.Key == Key.Enter)
                setSearchIncident();
        }
    }
}
